package com.ad.nbi.CSIRPSystem;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Properties;

import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import org.omg.PortableServer.IdAssignmentPolicyValue;
import org.omg.PortableServer.LifespanPolicyValue;
import org.omg.PortableServer.POA;
import org.omg.PortableServer.POAHelper;
import org.omg.PortableServer.ServantRetentionPolicyValue;
import org.omg.PortableServer.ThreadPolicyValue;

import CSIRPSystem.CSIRP;
import CSIRPSystem.CSIRPHelper;
import EPIRPSystem.EPIRP;
import EPIRPSystem.EPIRPHelper;
import NotificationIRPSystem.NotificationIRP;
import NotificationIRPSystem.NotificationIRPHelper;


public class Server {
	//持久化的POA  
    private static POA persistentPOA = null;
    
	public static void main(String[] args) {

		Properties props = new Properties();    
        //使用jacorb的CORBA实现方案  
        props.put("org.omg.CORBA.ORBClass", "org.jacorb.orb.ORB");  
        props.put("org.omg.CORBA.ORBSingletonClass", "org.jacorb.orb.ORBSingleton");  
        //使用持久化IOR，必须指定持久化POA的实现名称，默认是"StandardImplName"，  
        //可以随便指定  
        props.put("jacorb.implname", "StandardImplName");  
        //这里是指定CORBA服务器端端口为固定端口，是CORBA的IOR固定不变的关键  
        props.put("OAPort", "12500");  
//如果需要指定服务器端的ip地址，则需要使用下面这种方式，默认使用上面的方式//只指定端口即可，ip地址是服务器
//        props.put("OAAddress", "iiop://72.163.228.41");
        
		try {
		
			

            // 初始化ORB 
            org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args, props); 
            // 获取根POA引用 
            POA poaRoot = POAHelper.narrow(orb.resolve_initial_references("RootPOA")); 
            
            
            org.omg.CORBA.Policy[] policies = new org.omg.CORBA.Policy[3];  
            //POA生命周期是持久化  
            policies[0] = poaRoot.create_lifespan_policy(LifespanPolicyValue.PERSISTENT);  
            //CORBA对象的标识符是用户指定的  
            policies[1] = poaRoot.create_id_assignment_policy(IdAssignmentPolicyValue.USER_ID);  
            //每次接到一个请求时，POA期望应用程序提供目标对象标识符作为  
//查找伺服程序的索引  
            policies[2] = poaRoot.create_servant_retention_policy(ServantRetentionPolicyValue.RETAIN);  
            //创建持久化的POA  
            persistentPOA = poaRoot.create_POA("PersistentPOA", poaRoot.the_POAManager(), policies);  
                //清除策略    
            policies[0].destroy();  
            policies[1].destroy();  
            policies[2].destroy();  
            policies = null;  
            
            // 激活POA管理器
            poaRoot.the_POAManager().activate(); 
            // 实例化一个MyServiceServerImpl对象 
            CSIRPServerImpl serviceImpl = new CSIRPServerImpl(poaRoot); 
            
            //将实例并注册到ORB上 (用于复杂应用)
            serviceImpl.setOrb(orb); 
            
            byte[] servantId = "NotificationIRPServerImpl".getBytes();  
            //将伺服程序标识符和服务器端CORBA对象关联起来并激活  
            persistentPOA.activate_object_with_id(servantId, serviceImpl); 
            
            poaRoot.the_POAManager().activate();  
            
            // 从servant获得对象引用 
            org.omg.CORBA.Object refObj = poaRoot.servant_to_reference(serviceImpl); 
            
            System.out.println("CORBA IOR is:" + orb.object_to_string(refObj));
            // 获得对象接口引用 
            CSIRP href = CSIRPHelper.narrow(refObj); 
            // 根命名上下文  
//            org.omg.CORBA.Object ncObj = orb.resolve_initial_references("NameService"); 
//            NamingContextExt nc = NamingContextExtHelper.narrow(ncObj); 
            // 绑定一个服务引用,以便客户端可以调用
//            nc.rebind(nc.to_name("EPIRPSystem.EPIRP"), href); 
            
            System.out.println("MyService is running and waiting......"); 
            // 运行ORB 
            orb.run(); 
		}
		catch(Exception ex) {
			ex.printStackTrace();
		}
	}
}
